﻿using System;
using System.Collections.Specialized;
using System.Runtime.InteropServices;
using System.Text;

namespace TYBase
{
    public class INI
    {
        public string m_IniFile;

        [DllImport("kernel32")]
        private static extern bool WritePrivateProfileString(string section, string key, string val, string filePath);

        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string def, byte[] retVal, int size, string filePath);

        [DllImport("kernel32")] public static extern bool WritePrivateProfileString(byte[] section, byte[] key, byte[] val, string filePath);

        [DllImport("kernel32")] public static extern int GetPrivateProfileString(byte[] section, byte[] key, byte[] def, byte[] retVal, int size, string filePath);

        //与ini交互必须统一编码格式
        private static byte[] getBytes(string s, string encodingName)
        {
            return null == s ? null : Encoding.GetEncoding(encodingName).GetBytes(s);
        }

        private string ReadStringBase(string section, string key, string def, string fileName, string encodingName = "utf-8", int size = 1024)
        {
            byte[] buffer = new byte[size];
            int count = GetPrivateProfileString(getBytes(section, encodingName), getBytes(key, encodingName), getBytes(def, encodingName), buffer, size, fileName);
            return Encoding.GetEncoding(encodingName).GetString(buffer, 0, count).Trim();
        }

        private bool WriteStringBase(string section, string key, string value, string fileName, string encodingName = "utf-8")
        {
            return WritePrivateProfileString(getBytes(section, encodingName), getBytes(key, encodingName), getBytes(value, encodingName), fileName);
        }

        public INI(string fileName)
        {
            string IniFilePath = System.AppDomain.CurrentDomain.BaseDirectory;
            m_IniFile = IniFilePath + fileName;
        }

        //********************************************************************************************************************
        //********************************************************************************************************************
        public bool WriteStringEx(string Section, string Key, string Val)
        {
            bool bret = WriteStringBase(Section, Key, Val, m_IniFile); //Success--->nonzero.

            return bret;
        }

        public bool WriteString(string Key, string Val)
        {
            bool bret = WriteStringBase(strSection, Key, Val, m_IniFile); //Success--->nonzero.

            return bret;
        }

        public string ReadStringEx(string Section, string Key, string Default = "")
        {
            return ReadStringBase(Section, Key, Default, m_IniFile);
        }

        public string ReadString(string Key, string Default = "")
        {
            return ReadStringBase(strSection, Key, Default, m_IniFile);
        }

        //********************************************************************************************************************
        //********************************************************************************************************************
        public bool WriteFloat(string Section, string Key, float Val)
        {
            bool bret = WriteStringEx(Section, Key, Val.ToString());

            return bret;
        }

        public float ReadFloat(string Section, string Key, float Default)
        {
            string TmpStr = ReadStringEx(Section, Key, Default.ToString());

            try
            {
                return Convert.ToSingle(TmpStr);
            }
            catch (Exception ex)
            {
                return Default;
            }
        }//txtCkMeasVal

        //********************************************************************************************************************
        //********************************************************************************************************************
        public bool WriteDouble(string Section, string Key, double Val)
        {
            bool bret = WriteStringBase(Section, Key, Val.ToString(), m_IniFile);

            return bret;
        }

        public bool WriteDouble(string Key, double Val)
        {
            bool bret = WriteStringBase(strSection, Key, Val.ToString(), m_IniFile);

            return bret;
        }

        public double ReadDouble(string Section, string Key, double Default)
        {
            string TmpStr = ReadStringEx(Section, Key, Default.ToString());

            try
            {
                return Convert.ToDouble(TmpStr);
            }
            catch (Exception ex)
            {
                return Default;
            }
        }

        public double ReadDouble(string Key, double Default)
        {
            string TmpStr = ReadString(Key, Default.ToString());

            try
            {
                return Convert.ToDouble(TmpStr);
            }
            catch (Exception ex)
            {
                return Default;
            }
        }

        //********************************************************************************************************************
        //********************************************************************************************************************
        public bool WriteInt(string Section, string Key, int Val)
        {
            bool bret = WriteStringEx(Section, Key, Val.ToString());

            return bret;
        }

        public static string strSection = "SysPara";

        public bool WriteInt(string Key, int Val)
        {
            bool bret = WriteStringEx(strSection, Key, Val.ToString());

            return bret;
        }

        //********************************************************************************************************************
        //********************************************************************************************************************
        public bool WriteInt(string Section, string Key, uint Val)
        {
            bool bret = WriteStringEx(Section, Key, Val.ToString());

            return bret;
        }

        public int ReadIntEx(string Section, string Key, int Default = 0)
        {
            string TmpStr = ReadStringEx(Section, Key, Default.ToString());

            try
            {
                return Convert.ToInt32(TmpStr);
            }
            catch (Exception ex)
            {
                return Default;
            }
        }

        public int ReadInt(string Key, int Default = 0)
        {
            string TmpStr = ReadString(Key, Default.ToString());

            try
            {
                return Convert.ToInt32(TmpStr);
            }
            catch (Exception ex)
            {
                return Default;
            }
        }

        //********************************************************************************************************************
        //********************************************************************************************************************
        public bool WriteBool(string Section, string Key, bool Val)
        {
            bool bret = WriteStringEx(Section, Key, Val.ToString());

            return bret;
        }

        public bool WriteBool(string Key, bool Val)
        {
            bool bret = WriteStringEx(strSection, Key, Val.ToString());

            return bret;
        }

        public bool ReadBoolEx(string Section, string Key, bool Default)
        {
            string TmpStr = ReadStringEx(Section, Key, Default.ToString());

            try
            {
                return Convert.ToBoolean(TmpStr);
            }
            catch (Exception ex)
            {
                return Default;
            }
        }

        public bool ReadBool(string Key, bool Default = false)
        {
            string TmpStr = ReadStringEx(strSection, Key, Default.ToString());

            try
            {
                return Convert.ToBoolean(TmpStr);
            }
            catch (Exception ex)
            {
                return Default;
            }
        }

        ////********************************************************************************************************************
        ////********************************************************************************************************************

        ////删除指定Section
        //public bool IniEraseSection(string Section)
        //{
        //    bool bret = WritePrivateProfileString(Section, null, null, m_IniFile);

        //    return bret;
        //}

        ////删除指定Section下的键
        //public bool IniDeleteKey(string Section, string Key)
        //{
        //    bool bret = WritePrivateProfileString(Section, Key, null, m_IniFile);

        //    return bret;
        //}

        ////********************************************************************************************************************
        ////********************************************************************************************************************

        ////检查Section下的键值是否存在
        //public bool IniKeyExist(string Section, string Key)
        //{
        //    StringCollection KeyList = new StringCollection();
        //    ReadKeyList(Section, KeyList);

        //    return KeyList.IndexOf(Key) > -1;
        //}

        ////读取Section下的所有Key
        //private void ReadKeyList(string Section, StringCollection KeyList)
        //{
        //    Byte[] TmpBuf = new Byte[4096];

        //    int Len = GetPrivateProfileString(Section, null, null, TmpBuf, TmpBuf.GetUpperBound(0), m_IniFile);
        //    SplitBufToStrList(TmpBuf, Len, KeyList);
        //}

        //private void SplitBufToStrList(Byte[] Buffer, int bufLen, StringCollection StrList)
        //{
        //    StrList.Clear();

        //    if (bufLen != 0)
        //    {
        //        int start = 0;
        //        for (int i = 0; i < bufLen; i++)
        //        {
        //            if ((Buffer[i] == 0) && ((i - start) > 0))
        //            {
        //                String TmpStr = Encoding.GetEncoding(0).GetString(Buffer, start, i - start);
        //                StrList.Add(TmpStr);
        //                start = i + 1;
        //            }
        //        }
        //    }
        //}

        ////读取指定Section 的所有Key及Value列表
        //public void ReadKeyValList(string Section, NameValueCollection KeyValList)
        //{
        //    StringCollection KeyList = new StringCollection();

        //    ReadKeyList(Section, KeyList);
        //    KeyValList.Clear();

        //    foreach (string key in KeyList)
        //    {
        //        KeyValList.Add(key, ReadStringEx(Section, key, ""));
        //    }
        //}

        ////读取所有Sections名称
        //public void ReadSectionList(StringCollection SectionList)
        //{
        //    //必须得用Bytes来实现，StringBuilder只能取到第一个Section
        //    byte[] TmpStr = new byte[4096];
        //    int bufLen = 0;

        //    bufLen = ReadStringBase(null, null, null, m_IniFile);
        //    SplitBufToStrList(TmpStr, bufLen, SectionList);
        //}
    }
}